﻿
$.ajaxSetup({
    timeout: 20000,
    async: true,
    cache: false,
    complete: function () {
        showMask(false);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        // 默认值: 自动判断 (xml 或 html)。请求失败时调用此函数。
        // 有以下三个参数：XMLHttpRequest 对象、错误信息、（可选）捕获的异常对象。
        // 如果发生了错误，错误信息（第二个参数）除了得到 null 之外，还可能是 "timeout", "error", "notmodified" 和 "parsererror"。
        switch (textStatus) {
            case "timeout":
                $.messager.alert('系统出错', "返回超时，请重试。");
                break;
            case "pasererror":
                $.messager.alert('系统出错', "返回数据格式错误。");
                break;
            default:
                $.messager.alert('系统出错', textStatus);
                break;
        }
    }
});

var initSearch = function (item, menuItem, onSearch) {
    $(item).searchbox({
        searcher: function (value, name) {
            //$(".search").attr("searchfeild", name).attr("searchtype", "like").attr("searchkey", value);
            if (onSearch != undefined) {
                if (typeof onSearch == 'function') {
                    onSearch();
                }
            }
        },
        menu: menuItem,
        prompt: '输入查询条件'
    });
}

var setSelectAll = function () {
    $(".selectall").click(function () {
        $(".itemmark[value]").prop("checked", !$(".itemmark[value]").prop("checked"));
    });
}

var GetConstJson = function (constName, onsuccess) {
    $.ajax({
        beforeSend: function () {

        },
        type: "get",
        dataType: "json",
        url: "/app/GetConstList.ashx",
        data: { constname: constName },
        error: function () {
            //$.messager.alert('系统出错', ' 请重试!');
        },
        complete: function () {

        },
        success: function (result) {
            if (result.message == JsonResult.Success) {
                onsuccess(result.data);
            }
            else {
                
            }
        }
    });
}

var GetTName = function (jsonData, key) {
    var tname = "未知(" + key + ")";
    $.each(jsonData, function (i, n) {
        if (n.Value == key) {
            tname = n.Name;
            return tname;
        }
    });
    return tname;
}

var GetTDescription = function (jsonData, key) {
    //var tname = "未知(" + key + ")";
    var tname = key;
    $.each(jsonData, function (i, n) {
        if (n.Value == key) {
            tname = n.Description;
            return tname;
        }
    });
    return tname;
}

var SetSelectWithJson = function (sel, jsonData) {
    $(sel + " option[value!='']").remove();
    $.each(jsonData, function (i, n) {
        //if (i == jsonData.length - 1) {
        if (i == 0) { //默认选中第一个选项
            $(sel).append("<option value='" + n.Value + "' selected='selected'>" + n.Description + "</option>");
        }
        else {
            $(sel).append("<option value='" + n.Value + "'>" + n.Description + "</option>");
        }
    });

    //$(sel).val($(sel).find("option:last").val());
    // $(sel + " option:last").attr("selected", "selected"); //必须直接在HTML标签中写入，设置attr的方式在IE中存在问题,会导致后台获取不到初始值

    //alert($(sel).val());
}

var SetSelectValueWithJson = function (sel, jsonData) {
    $(sel + " option[value!='']").remove();
    $.each(jsonData, function (i, n) {
        //if (i == jsonData.length - 1) {
        if (i == 0) { //默认选中第一个选项
            $(sel).append("<option value='" + n.Value + "' selected='selected'>" + n.Value + "</option>");
        }
        else {
            $(sel).append("<option value='" + n.Value + "'>" + n.Value + "</option>");
        }
    });

}

var SetSelect = function (sel, value) {
    //alert("|ss|" + sel + "||" + value + "||");

    $(sel).each(function () {
        $(this).find("option").removeAttr("selected");
        //$(this).find("option[value=" + value + "]").attr("selected", "selected");
        $(this).find("option").each(function () {
            //alert("||" + $(this).val() + "||" + value + "||");
            if ($(this).val() == value) {
                $(this).prop("selected", true);
                return false; //break
            }
        });
    });
}

var showMask = function (show, parentId) {
    //maskpanel
    if (show) {
        if ($(".maskpanel").size() == 0) {
            $("<div class='maskpanel'><div class='loading'><span>数据读取中...</span></div></div>").appendTo($(parentId + ":first"));
        }
        var offset = $(parentId + ":first").offset();
        $(".maskpanel").css({ "width": $(parentId + ":first").width() + 2, "height": $(parentId + ":first").height() + 2, left: offset.left, top: offset.top }).show();
        $(".loading").css({ left: ($(".maskpanel").width() - $(".loading").width()) / 2, top: ($(".maskpanel").height() - $(".loading").height()) / 2 });
    }
    else {
        $(".maskpanel").hide();
    }

    return;
}

var SetConsol = function (html) {
    $(".conlineState").html(html);
}

var SetGridCSelect = function (item) {
    $(item).mouseover(function () {
        $(this).addClass("hover");
    }).mouseleave(function () {
        $(this).removeClass("hover");
    }).click(function () {
        if ($(this).hasClass("emptyrow")) {
            return;
        }
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        }
        else {
            $(this).parent().children().removeClass("selected");
            $(this).addClass("selected");
        }
    });
}

var ClearCrossPageSelect = function (groupName) {
    if (typeof (_crossPageData) == "undefined") {
        _crossPageData = new Array();
    }
    for (var j = 0; j < _crossPageData.length; j++) {
        if (_crossPageData[j][0] == groupName) {
            _crossPageData[j].splice(1, _crossPageData[j].length - 1);
            break;
        }
    }
}

var GetCrossPageSelect = function (groupName, autoBind) {
    if (autoBind) {
        SetCrossPageSelect(groupName, false);
    }
    var data = ""
    if (typeof (_crossPageData) == "undefined") {
        _crossPageData = new Array();
    }
    for (var j = 0; j < _crossPageData.length; j++) {
        if (_crossPageData[j][0] == groupName) {
            for (var k = 1; k < _crossPageData[j].length; k++) {
                data = data + _crossPageData[j][k] + ",";
            }
            if (data.length > 0) {
                data = data.substring(0, data.length - 1);
            }
            break;
        }
    }
    return data;
}

var SetCrossPageSelect = function (groupName, isBind) {
    if (typeof (_crossPageData) == "undefined") {
        _crossPageData = new Array();
    }
    var i = -1;
    for (var j = 0; j < _crossPageData.length; j++) {
        if (_crossPageData[j][0] == groupName) {
            i = j;
            break;
        }
    }
    if (i == -1) {
        i = _crossPageData.length;
        _crossPageData[i] = new Array();
        _crossPageData[i][0] = groupName;
    }

    if (isBind) {
        $(".itemmark[group='" + groupName + "']").attr("checked", false)
        .each(function () {
            var val = $(this).val();
            for (var j = 1; j < _crossPageData[i].length; j++) {
                if (_crossPageData[i][j] == val) {
                    $(this).prop("checked", true);
                    break; //return true; // false时相当于break, 如果return true 就相当于continure。
                }
            }
           $(this).attr("checked", j != -1);
        });
    } else {
        $(".itemmark[group='" + groupName + "'][value]").each(function () {
            var val = $(this).val();
            var add = true;
            for (var j = 1; j < _crossPageData[i].length; j++) {
                if (_crossPageData[i][j] == val) {
                    add = false;
                    if (!$(this).prop("checked")) {
                        //alert(_crossPageData[i][j]);
                        _crossPageData[i].splice(j, 1);
                        break;
                    }
                }
            }
            if (add && $(this).prop("checked")) {
                _crossPageData[i].push(val);
            }
        });

    }
}

//var OpenFingerCach = function () {
//    window.open('/app/SubmitFinger.aspx', 'newwindow', 'height=150,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
//}

// autoCheck是否验证  待验证的用户no
var loadFingerSubmit = function (autoCheck, empno) {
    if ($("#fingerSubmit").size() == 0) {
        $("<div id='fingerSubmit'></div>").appendTo(document.body);
    }
    //alert($(this).html());
    if ($("#fingerSubmit").html() == "") {
        //$(this).linkbutton("disable");
        $("#fingerSubmit").load("/app/SubmitFinger.aspx", function () {
            if (autoCheck) {
                $("#checkfinger").val("check")
            }
            if (empno) {
                $("#empno").val(empno)
            }
            //$(this).linkbutton("enable");
        });
    } else {
        $('#submitfinger').window('open');
    }
}

//格式化为 YYYY-MM-DD
Date.prototype.toDayString = function () {
    return this.getYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
}
//格式化为 YYYY-MM-DD HH:mm:ss
Date.prototype.toMinutesString = function () {
    return this.getYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + " "
                + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
}
//获取URL参数
var request = function (paras) {
    var url = location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        return returnValue.replace("#", "");
    }
}